/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */

package com.andremion.floatingnavigationview.sample.slice;

import com.andremion.floatingnavigationview.CircularRevealNavigationView;
import com.andremion.floatingnavigationview.sample.ResourceTable;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;

import ohos.agp.components.AbsButton;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.RadioButton;
import ohos.agp.window.dialog.ToastDialog;
import ohos.agp.window.service.WindowManager;

/**
 * MainAbilitySlice
 *
 * @author hw
 * @since 2021/5/24
 */
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener,
        AbsButton.CheckedStateChangedListener {
    private CircularRevealNavigationView circularRevealNavigationView;
    private RadioButton rbtnImport;
    private RadioButton rbtnGallery;
    private RadioButton rbtnSlideshow;
    private RadioButton rbtnTools;
    private Button btSend;
    private Button btShare;
    private ToastDialog toastDialog;

    @Override
    public void onStart(Intent intent) {
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);   // 沉浸式状态栏
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initComponent();
    }

    private void initComponent() {
        toastDialog = new ToastDialog(this);
        if (findComponentById(ResourceTable.Id_cr_layout) instanceof CircularRevealNavigationView) {
            circularRevealNavigationView = (CircularRevealNavigationView) findComponentById(ResourceTable.Id_cr_layout);
        }
        if (findComponentById(ResourceTable.Id_bt_send) instanceof Button) {
            btSend = (Button) findComponentById(ResourceTable.Id_bt_send);
        }
        if (findComponentById(ResourceTable.Id_bt_share) instanceof Button) {
            btShare = (Button) findComponentById(ResourceTable.Id_bt_share);
        }

        btSend.setClickedListener(this);
        btShare.setClickedListener(this);

        if (findComponentById(ResourceTable.Id_rb_import) instanceof RadioButton) {
            rbtnImport = (RadioButton) findComponentById(ResourceTable.Id_rb_import);
        }
        if (findComponentById(ResourceTable.Id_rb_gallery) instanceof RadioButton) {
            rbtnGallery = (RadioButton) findComponentById(ResourceTable.Id_rb_gallery);
        }
        if (findComponentById(ResourceTable.Id_rb_slideshow) instanceof RadioButton) {
            rbtnSlideshow = (RadioButton) findComponentById(ResourceTable.Id_rb_slideshow);
        }
        if (findComponentById(ResourceTable.Id_rb_tool) instanceof RadioButton) {
            rbtnTools = (RadioButton) findComponentById(ResourceTable.Id_rb_tool);
        }

        lastCheckId = rbtnImport.getId();
        rbtnImport.setCheckedStateChangedListener(this);
        rbtnGallery.setCheckedStateChangedListener(this);
        rbtnSlideshow.setCheckedStateChangedListener(this);
        rbtnTools.setCheckedStateChangedListener(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_bt_share:
                circularRevealNavigationView.startAnimal();
                toastDialog.setText(getString(ResourceTable.String_menu_item_share));
                toastDialog.setDuration(2000).show();
                break;
            case ResourceTable.Id_bt_send:
                circularRevealNavigationView.startAnimal();
                toastDialog.setText(getString(ResourceTable.String_menu_item_send));
                toastDialog.setDuration(2000).show();
                break;
        }
    }

    @Override
    protected void onBackPressed() {
        if (circularRevealNavigationView != null && circularRevealNavigationView.isOpenMenu()) {
            circularRevealNavigationView.startAnimal();
        } else {
            super.onBackPressed();
        }
    }

    private int lastCheckId = 0; // 记录上一次选中的radioButton的id

    @Override
    public void onCheckedChanged(AbsButton absButton, boolean check) {
        if (absButton.getId() == lastCheckId && !check) {   // 同一个button在checked状态下不能改变为unchecked
            absButton.setChecked(true);
        } else if (absButton.getId() != lastCheckId && check) {
            lastCheckId = absButton.getId();
        } else {
            return;
        }
        circularRevealNavigationView.startAnimal();
        toastDialog.setText(absButton.getText());
        toastDialog.setDuration(2000).show();
    }
}
